<?php

namespace addon\hotel\api\controller;

use addon\hotel\model\Config as ConfigModel;
use addon\hotel\model\Room;
use addon\hotel\model\Room as RoomModel;
use app\api\controller\BaseApi;
use app\model\hotel\Baoyu;
use app\model\hotel\Hotel as HotelModel;


class Hotel extends BaseApi
{

    //入驻协议
    public function agreement()
    {
        //入驻协议
        $config_model = new ConfigModel();
        $document_info = $config_model->getSettledInDocument();
        return $this->response($this->success($document_info['data']));
    }


    //首页酒店
    public function index()
    {
        $hotel_model = new HotelModel();

        $condition = [
            ['status','=',1],
            ['is_recommend','=',1]
        ];

        $field = 'hotel_id,hotel_name,hotel_img,recommend_reason,address,full_address,longitude,latitude';

        $list = $hotel_model->getHotelList($condition,$field,'sort asc');

        foreach ($list['data'] as $k=>$v){
            $room_list = model('hotel_room')->getColumn(['hotel_id'=>$v['hotel_id'],'status'=>1],'price');
            if (count($room_list)==0){
                unset($list['data'][$k]);
            }
        }
        return $this->response($list);
    }

    //酒店列表
    public function page()
    {
        $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
        $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
        $lat = isset($this->params[ 'lat' ]) ? $this->params[ 'lat' ] : ""; // 纬度
        $lng = isset($this->params[ 'lng' ]) ? $this->params[ 'lng' ] : ""; // 经度
        $order = isset($this->params[ 'order' ]) ? $this->params[ 'order' ] : "site_id";//排序（综合、销量、信用）
        $sort = isset($this->params[ 'sort' ]) ? $this->params[ 'sort' ] : "desc";//升序、降序
        $hotel_level = isset($this->params[ 'hotel_level' ]) ? $this->params[ 'hotel_level' ] : 0;
        $keyword = isset($this->params[ 'keyword' ]) ? $this->params[ 'keyword' ] : "";

        $hotel_model = new HotelModel();

        $condition = [
            ['status','=',1],
            //['is_recommend','=',1]
        ];

        if ($hotel_level){
            $condition[] = ['hotel_level','=',$hotel_level];
        }

        if ($keyword){
            $condition[] = ['hotel_name', 'like', "%". $keyword ."%"];
        }

        if ($order=='price') {
            $orderBy = 'room_min_price '.$sort;
        } else {
            $orderBy = 'sort asc';
        }

        $field = 'hotel_id,hotel_name,hotel_img,recommend_reason,address,full_address,longitude,latitude,direct';

        $list = $hotel_model->getHotelPageList($condition,$page,$page_size,$orderBy,$field);

        $token = $this->checkToken();

        $room_model = new RoomModel();

        if (!empty($list[ 'data' ][ 'list' ])) {
            foreach ($list[ 'data' ][ 'list' ] as $k => $item) {

                if ($item[ 'longitude' ] && $item[ 'latitude' ] && $lng && $lat) {
                    $list[ 'data' ][ 'list' ][ $k ][ 'distance' ] = round(getDistance((float) $item[ 'longitude' ], (float) $item[ 'latitude' ], (float) $lng, (float) $lat));
                } else {
                    $list[ 'data' ][ 'list' ][ $k ][ 'distance' ] = 0;
                }

                //查酒店所拥有的房间
                $room_list = model('hotel_room')->getColumn(['hotel_id'=>$item['hotel_id'],'status'=>1],'price');
                if (count($room_list)>0){
                    $start_price = min($room_list);

                    $room_info =  model('hotel_room')->getInfo(['price'=>$start_price],'room_id');

                    if ($token['code'] >= 0){
                        // 是否参与会员等级折扣
                        $goods_member_price = $room_model->getRoomPrice($room_info['room_id'], $this->member_id);
                        $goods_member_price = $goods_member_price['data'];
                        if (!empty($goods_member_price['member_price'])) {
                            $list[ 'data' ][ 'list' ][ $k ]['member_price'] = $goods_member_price['member_price'];
                        }
                    }

                    $list[ 'data' ][ 'list' ][ $k ][ 'start_price' ] = min($room_list);

                    model('hotel')->update(['room_min_price'=>min($room_list)],[['hotel_id','=',$item['hotel_id']]]);

                } else {
                    unset($list[ 'data' ][ 'list' ][$k]);
                }
            }
            $list[ 'data' ][ 'list' ] = array_values($list[ 'data' ][ 'list' ]);
            if ($order=='distance'){
                if ($sort=='asc'){
                    // 按距离就近排序
                    array_multisort(array_column($list[ 'data' ][ 'list' ], 'distance'), SORT_ASC, $list[ 'data' ][ 'list' ]);
                } else {
                    // 按距离就近排序
                    array_multisort(array_column($list[ 'data' ][ 'list' ], 'distance'), SORT_DESC, $list[ 'data' ][ 'list' ]);
                }
            }

        }
        return $this->response($list);
    }


    //酒店详情
    public function info()
    {
        $hotel_id = isset($this->params[ 'hotel_id' ]) ? $this->params[ 'hotel_id' ] : 0;
        $lat = isset($this->params[ 'lat' ]) ? $this->params[ 'lat' ] : ""; // 纬度
        $lng = isset($this->params[ 'lng' ]) ? $this->params[ 'lng' ] : ""; // 经度

        $hotel_model = new HotelModel();

        $condition = [
            ['hotel_id','=',$hotel_id]
        ];

        $field = 'hotel_id,hotel_name,hotel_img,traffic,hotel_level,start_time,end_time,images,video_url,introduce,telphone,direct,
        address,full_address,longitude,latitude,label_format,equipment_format,free_cancel_time,refund_rule';

        $hotel_info = $hotel_model->getHotelInfo($condition,$field);

        if ($hotel_info['data'][ 'longitude' ] && $hotel_info['data'][ 'latitude' ] && $lng && $lat) {
            $hotel_info['data'][ 'distance' ] = round(getDistance((float) $hotel_info['data'][ 'longitude' ], (float) $hotel_info['data'][ 'latitude' ], (float) $lng, (float) $lat));
        } else {
            $hotel_info['data'][ 'distance' ] = 0;
        }
        $hotel_info['data'][ 'label_format' ] = json_decode($hotel_info['data'][ 'label_format' ],true);
        $hotel_info['data'][ 'equipment_format' ] = json_decode($hotel_info['data'][ 'equipment_format' ],true);

        $hotel_info['data'][ 'free_cancel_time' ] = date('H:i', strtotime(date('Y-m-d')) + $hotel_info['data']['free_cancel_time']);
        return $this->response($hotel_info);
    }



}